package com.web.listener;

import com.commons.Constants;
import com.pojo.Users;

import javax.servlet.ServletContext;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

/*
* 解决httpsession被反复销毁的问题
* */
@WebListener
public class HttpSessionLifecycleListener implements HttpSessionListener {
    @Override
    public void sessionCreated(HttpSessionEvent se) {
        HttpSessionListener.super.sessionCreated(se);
    }



    @Override
    public void sessionDestroyed(HttpSessionEvent se) {
        //获取servletContext对象，然后将所对应的httpsession删掉
        HttpSession httpSession=se.getSession();
        ServletContext servletContext = httpSession.getServletContext();
        Users users= (Users) httpSession.getAttribute(Constants.USER_SESSION_KEY);
        servletContext.removeAttribute(users.getUserid()+"");
    }
}
